import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/a_elder/page/elder_content_page.dart';
import 'package:ls_work/a_yyt/models/health_record_entity.dart';
import 'package:ls_work/a_yyt/presenter/health_record_presenter.dart';
import 'package:ls_work/a_yyt/widget/health_record_cell.dart';
import 'package:ls_work/mvp/base_page_state.dart';
import 'package:ls_work/provider/base_list_provider.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/ui/select_old_page.dart';
import 'package:ls_work/util/app_navigator.dart';
import 'package:ls_work/widgets/my_refresh_divider_list.dart';
import 'package:provider/provider.dart';

class HealthRecordPage extends StatefulWidget {
  final ElderInfo? info;

  HealthRecordPage({this.info});

  @override
  HealthRecordPageState createState() => HealthRecordPageState();
}

class HealthRecordPageState
    extends BasePageState<HealthRecordPage, HealthRecordPresenter>
    with AutomaticKeepAliveClientMixin {
  BaseListProvider<HealthRecordInfo> provider =
      BaseListProvider<HealthRecordInfo>();

  int _page = 1;
  int? customerId;
  String _keyword = '';
  String _name = '选择老人';

  @override
  void initState() {
    if(widget.info!=null){
      setState(() {
        customerId = widget.info?.customerId;
        _name = widget.info?.name??"";
      });
    }
    WidgetsBinding.instance.addPostFrameCallback((_) {
      /// 接口请求
      print(presenter);
      presenter.fectData(_keyword, _page, true, customerId);
    });
    super.initState();
  }

  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<BaseListProvider<HealthRecordInfo>>(
      create: (_) => provider,
      child: Consumer<BaseListProvider<HealthRecordInfo>>(
          builder: (_, provider, __) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _elderNameFect,
            Expanded(
                child: DeerSeparatedListView(
              key: Key('follow_warning_list'),
              itemCount: provider.list.length,
              stateType: provider.stateType,
              onRefresh: _onRefresh,
              loadMore: _loadMore,
              hasMore: provider.hasMore,
              itemBuilder: (_, index) {
                return ExpansionPanelList.radio(
                  expansionCallback: (int index, bool isExpanded) {
                    // setState(() {
                    provider.list[index].isExpanded = isExpanded;
                    print(provider.list[index].isExpanded);
                    // });
                  },
                  children: [
                    ExpansionPanelRadio(
                        value: provider.list[index].isExpanded==true,
                        canTapOnHeader: true,
                        headerBuilder: (BuildContext context, bool isExpanded) {
                          return ListTile(
                            title: customerId != null
                                ? Text(provider.list[index].collectionTime??"")
                                : RichText(
                                    text: TextSpan(children: [
                                    TextSpan(
                                        text:
                                            '${provider.list[index].customerName}',
                                        style: TextStyle(color: Colors.orange)),
                                    TextSpan(
                                        text:
                                            '   ${provider.list[index].collectionTime}',
                                        style: TextStyles.textDark16)
                                  ])),
                          );
                        },
                        body: HealthRecordCell(
                          info: provider.list[index],
                        ))
                  ],
                );
                // return HealthRecordCell(
                //   info: provider.list[index],
                // );
              },
              separatorBuilder: (_, int index) {
                return Container(
                  width: double.infinity,
                  height: 1,
                  color: Colours.bg_color,
                );
              },
            ))
          ],
        );
      }),
    );
  }

  Widget get _elderNameFect {
    return Container(
        width: ScreenUtil.getScreenW(context),
        alignment: Alignment.center,
        height: 60,
        decoration: BoxDecoration(
            border: Border(
          bottom:
              Divider.createBorderSide(context, color: Colours.line, width: 1),
        )),
        child: Container(
          color: Colours.bg_color,
          width: 125,
          height: 40,
          child: InkWell(
            onTap: () {
              Go().push(SelectOldPage((info) {
                Go().pop();
                if (info.name == _name) return;
                setState(() {
                  _name = info.name??"";
                  customerId = info.id;
                });
                _onRefresh();
              }));
              // AppNavigator.push(
              //     context,
              //     ElderContentPage(
              //       type: 0,
              //       isSelected: true,
              //       cellClickBlocks: (info) {
              //         setState(() {
              //           // elderInfo = info;
              //           if (info.name == _name) return;
              //           _name = info.name;
              //           customerId = info.id;
              //           _onRefresh();
              //         });
              //       },
              //     ));
            },
            child: Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Text(
                    _name,
                    textAlign: TextAlign.center,
                    style: TextStyles.textGray16,
                  ),
                ),
                Icon(
                  Icons.arrow_drop_down,
                  color: Colours.text_gray,
                )
              ],
            ),
          ),
        ));
  }

  Future _onRefresh() async {
    _page = 1;
    await presenter.fectData(_keyword, _page, true, customerId);
  }

  Future _loadMore() async {
    _page++;
    await presenter.fectData(_keyword, _page, true, customerId);
  }

  @override
  HealthRecordPresenter createPresenter() {
    return HealthRecordPresenter();
  }
}
